﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Web.User.OtherOperate
{
    public partial class DepositorClosing : System.Web.UI.Page
    {
        public double numTotol = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Account"] != null)
            {
                GetDepositor(Request.QueryString["Account"].ToString());
            }
        }
        private void GetDepositor(string AccountNumber)
        {


            StringBuilder strSql = new StringBuilder();
            strSql.Append("   select ID,WBID,AccountNumber,strPassword,CunID as BD_Address_CunID,strAddress,strName,PhoneNO,ISSendMessage,BankCardNO,dt_Update,");
            strSql.Append("   CASE( numState) WHEN 1 THEN '正常' ELSE '挂失' END AS numState,dt_Add,");
            strSql.Append("   CASE (IDCard) WHEN '' THEN '未填写' ELSE '******' END as IDCard");
            strSql.Append(" FROM dbo.Depositor ");

            strSql.Append(" where AccountNumber=@AccountNumber ");
            SqlParameter[] parameters = {
					new SqlParameter("@AccountNumber", SqlDbType.NVarChar,50)};
            parameters[0].Value = AccountNumber;
            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString(), parameters);
            if (dt != null && dt.Rows.Count != 0)
            {
                D_strName.InnerText = dt.Rows[0]["strName"].ToString();
                D_strAddress.InnerText = dt.Rows[0]["strAddress"].ToString();
                D_PhoneNo.InnerText = dt.Rows[0]["PhoneNo"].ToString();
                D_AccountNumber.InnerText = dt.Rows[0]["AccountNumber"].ToString();
                D_numState.InnerText = dt.Rows[0]["numState"].ToString();
                D_IDCard.InnerText = dt.Rows[0]["IDCard"].ToString();
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "ShowDepositorInfo()", true);
            }
            else
            {
                string StrScript;
                StrScript = ("<script language=javascript>");
                StrScript += ("alert('您查询的储户不存在!');");
                StrScript += ("</script>");
                System.Web.HttpContext.Current.Response.Write(StrScript);
            }

            //获取存粮信息
            StringBuilder strSqlStorage = new StringBuilder();
            strSqlStorage.Append("SELECT A.ID,A.StorageNumber,convert(varchar(10),A.StorageDate,120) AS StorageDate, A.AccountNumber,B.strName AS VarietyID,A.Price_ShiChang,A.Price_DaoQi,A.CurrentRate,C.strName AS TimeID,A.StorageFee");
            strSqlStorage.Append("  FROM dbo.Dep_StorageInfo A INNER JOIN dbo.StorageVariety B ON A.VarietyID=B.ID");
            strSqlStorage.Append("  INNER JOIN dbo.StorageTime C ON A.TimeID=C.ID");
            strSqlStorage.Append("  WHERE AccountNumber='" + AccountNumber + "'");
            DataTable dtStorage = SQLHelper.ExecuteDataTable(strSqlStorage.ToString());
            if (dtStorage != null && dtStorage.Rows.Count != 0)
            {
                Repeater1.DataSource = dtStorage;
                Repeater1.DataBind();
            }

        }


        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (QAccountNumber.Value.Trim() != "")
            {
                GetDepositor(QAccountNumber.Value.Trim());
            }
        }

        public string GetDay(object date)
        {
            DateTime t1 = Convert.ToDateTime(date);
            TimeSpan ts = DateTime.Now.Subtract(t1);
            int numday = Convert.ToInt32(Math.Floor((decimal)ts.TotalDays));
            return numday.ToString();

        }

        public string GetLiXi(object obj)
        {

            string strReturn = "";
            double Interest = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select B.InterestType,StorageDate,B.numStorageDate,WeighNo,StorageNumber,StorageFee,CurrentRate,Price_ShiChang,Price_DaoQi,Price_HeTong ");
            strSql.Append(" FROM dbo.Dep_StorageInfo A INNER JOIN dbo.StorageTime B ON A.TimeID=B.ID");
            strSql.Append(" WHERE A.ID=" + obj.ToString());
            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());
            if (dt != null && dt.Rows.Count != 0)
            {
                int InterestType = Convert.ToInt32(dt.Rows[0]["InterestType"]);//利息计算方式
                int StorageNumber = Convert.ToInt32(dt.Rows[0]["StorageNumber"]);//存储数量
                DateTime StorageDate = Convert.ToDateTime(dt.Rows[0]["StorageDate"]);//存入日期
                TimeSpan ts = DateTime.Now.Subtract(StorageDate);
                int numStorageDate = Convert.ToInt32(dt.Rows[0]["numStorageDate"]);//约定存储时间
                double CurrentRate = Convert.ToDouble(dt.Rows[0]["CurrentRate"]);//活期利率
                double Price_ShiChang = Convert.ToDouble(dt.Rows[0]["Price_ShiChang"]);//市场价
                double Price_DaoQi = Convert.ToDouble(dt.Rows[0]["Price_DaoQi"]);//到期价
                switch (InterestType)
                {
                    case 1://按月结息方式
                        Interest = StorageNumber * CurrentRate * ts.TotalDays / (double)30;
                        strReturn = "<span style='color:Blue;'>￥" + Math.Round(Interest, 2).ToString() + "</span>";
                        break;
                    case 2://按市场价结息方式
                        if (ts.TotalDays < numStorageDate)
                        {
                            strReturn = "<span style='font-size:12px;color:#888'>存期" + numStorageDate + "天,尚未到期</span>";
                        }
                        else
                        {
                            //计算当前的市场价格
                            StringBuilder strSqlShiChang = new StringBuilder();
                            strSqlShiChang.Append(" SELECT B.Price_ShiChang,B.EarningRate");
                            strSqlShiChang.Append(" FROM dbo.Dep_StorageInfo A INNER JOIN dbo.StorageRate B ON A.StorageRateID=B.ID");
                            strSqlShiChang.Append(" WHERE A.ID=" + obj.ToString());
                            DataTable dtShiChang = SQLHelper.ExecuteDataTable(strSqlShiChang.ToString());
                            if (dtShiChang != null && dtShiChang.Rows.Count != 0)
                            {
                                double Price_JieCun = Convert.ToDouble(dtShiChang.Rows[0]["Price_ShiChang"]);
                                int EarningRate = Convert.ToInt32(dtShiChang.Rows[0]["EarningRate"]);
                                if (Price_JieCun >= Price_ShiChang)//到期的结存价格比现在的市场价高
                                {
                                    Interest = (Price_JieCun - Price_ShiChang) * StorageNumber * EarningRate / (double)100;
                                    strReturn = "<span style='color:Blue;'>￥" + Math.Round(Interest, 2).ToString() + "</span>";
                                }
                                else
                                {
                                    Interest = -(Price_ShiChang - Price_JieCun) * StorageNumber;
                                    strReturn = "<span style='color:Red;'>￥" + Math.Round(Interest, 2).ToString() + "</span>";
                                }
                            }
                        }
                        break;
                    case 3:
                        if (ts.TotalDays < numStorageDate)
                        {
                            strReturn = "<span style='font-size:12px;color:#888'>存期" + numStorageDate + "天,尚未到期</span>";
                        }
                        else
                        {
                            Interest = (Price_DaoQi - Price_ShiChang) * StorageNumber;
                            strReturn = "<span style='color:Blue;'>￥" + Math.Round(Interest, 2).ToString() + "</span>";
                        }
                        break;
                }
                numTotol = numTotol + StorageNumber * Price_ShiChang + Interest;
            }
            numTotol = Math.Round(numTotol, 2);
            return strReturn;

        }
    }
}